﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WGMClient.Core;
using System.Threading;

namespace WGMClient.HelperClasses
{
    public static class Registry
    {
        public static MainClientWindow MainWindow { get; set; }
        public static ClientEngine Engine { get; set; }
        public static string Username { get; set; }
        public static string Password { get; set; }
        public static WGMLocalClient MyInfo { get; set; }
        private static Mutex _listMutex = new Mutex();
        public static string MacAddress { get; set; }
        public static System.Windows.Forms.MenuItem PopupTrayMenu                 ;
        public static System.Windows.Forms.MenuItem PopupTrayMenuNewUser       ;   
        public static System.Windows.Forms.MenuItem PopupTrayMenuLeavingUser     ;
        public static System.Windows.Forms.MenuItem PopupTrayMenuNewMessage;
        public static WGMSettings Settings { get; set; }
        public static string WorkingDir;
        public static string SettingsFile;
        public static string CurrentVersion;
        private static List<IMessageListener> _messageListeners = new List<IMessageListener>();
        public static ClientState MyState { get; set; }

        public static void RegisterListener(IMessageListener listener)
        {
            if(!_messageListeners.Contains(listener))
            {
                _messageListeners.Add(listener);
            }
        }
        public static void UnregisterListener(IMessageListener listener)
        {
            if (_messageListeners.Contains(listener))
            {
                _messageListeners.Remove(listener);
            }
        }
        public static void NotifyListeners(WGMSvc.WGMMessage message)
        {
            RemoveDeadListeners();
            for (int count = 0; count < _messageListeners.Count; count++ )
            {
                _messageListeners[count].NotifyServerMessage(message);
            }
        }
        public static void NotifyListeners(LocalMessage message)
        {
            RemoveDeadListeners();
            for (int count = 0; count < _messageListeners.Count; count++)
            {
                _messageListeners[count].NotifyLocalMessage(message);
            }
        }
        private static void RemoveDeadListeners()
        {
            for (int count = _messageListeners.Count; count > 0; count--)
            {
                if (_messageListeners[count - 1] == null)
                    _messageListeners.RemoveAt(count - 1);
            }
        }
    }

    public interface IMessageListener
    {
        void NotifyServerMessage(WGMSvc.WGMMessage message);
        void NotifyLocalMessage(LocalMessage message);
    }
    public enum LocalMessageTypes
    {
        CONNECTION_FAULTED,
        CONNECTION_RESTORED,
        SYSTEM_OPTIONS_CHANGED,
        MY_INFO_CHANGED,
        MY_IMAGE_CHANGED,
        GENERIC_MESSAGE
    }
    public class LocalMessage
    {
        public LocalMessageTypes MessageType { get; set; }
        public object MessageData { get; set; }
    }
}
